<template>
  <div class="page-container">
    <el-card class="main-card">
      <div slot="header" class="card-header">
        <span>角色管理</span>
        <div class="header-operations">
          <!-- 移除添加角色按钮 -->
        </div>
      </div>
      
      <el-table
        :data="roleList"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="roleName" label="角色名称" width="150"></el-table-column>
        <el-table-column prop="roleType" label="角色标识" width="150"></el-table-column>
        <el-table-column prop="description" label="角色描述"></el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <!-- 移除编辑、权限设置和删除按钮，添加启用/停用按钮 -->
            <el-button 
              type="text" 
              @click="handleChangeStatus(scope.row)"
              :disabled="scope.row.roleType === 'admin' && scope.row.status === 'active'"
              :title="scope.row.roleType === 'admin' && scope.row.status === 'active' ? '管理员角色不能被禁用' : ''">
              {{ scope.row.status === 'active' ? '停用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getRoleList, changeRoleStatus } from '@/api/user'

export default {
  name: 'UserRoles',
  data() {
    return {
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      roleList: []
    }
  },
  methods: {
    // 格式化日期显示
    formatDate(dateStr) {
      if (!dateStr) return '无';
      const date = new Date(dateStr);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    },
    
    // 修改角色状态
    handleChangeStatus(row) {
      // 如果是管理员角色且当前为启用状态，不允许禁用
      if (row.roleType === 'admin' && row.status === 'active') {
        this.$message.warning('系统管理员角色不能被禁用')
        return
      }
      
      const newStatus = row.status === 'active' ? 'locked' : 'active'
      const displayStatus = newStatus === 'active' ? '启用' : '停用'
      
      this.$confirm(`确认${displayStatus}该角色吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        
        // 构建完整的角色对象
        const updatedRole = {
          id: row.id,
          roleName: row.roleName,
          roleType: row.roleType,
          description: row.description,
          status: newStatus,
          createTime: row.createTime
        }
        
        changeRoleStatus(updatedRole).then(
          // eslint-disable-next-line no-unused-vars
          _ => {
          this.$message.success('状态修改成功')
          row.status = newStatus
        }).catch(err => {
          console.error('修改状态失败:', err)
          // 错误会由全局拦截器处理
        }).finally(() => {
          this.loading = false
        })
      }).catch(() => {})
    },
    
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    
    fetchData() {
      this.loading = true
      
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize
      }
      
      getRoleList(params).then(res => {
        this.roleList = res.data.records
        this.total = res.data.total
        console.log('角色列表数据:', this.roleList)
      }).catch(err => {
        console.error('获取角色列表失败:', err)
      }).finally(() => {
        this.loading = false
      })
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
  background: #f0f2f5;
  min-height: calc(100vh - 120px);
}

.main-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #304156;
}

.header-operations {
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* Element UI 主题覆盖 */
:deep(.el-button--primary) {
  background-color: #ff6b00;
  border-color: #ff6b00;
}

:deep(.el-button--primary:hover),
:deep(.el-button--primary:focus) {
  background-color: #ff8533;
  border-color: #ff8533;
}

:deep(.el-button--text) {
  color: #ff6b00;
}

:deep(.el-button--text:hover),
:deep(.el-button--text:focus) {
  color: #ff8533;
}

/* 禁用状态的文字按钮样式 */
:deep(.el-button--text.is-disabled) {
  color: #cfcfcf !important;
  cursor: not-allowed;
  opacity: 0.8;
}

/* 移除管理员角色的禁用按钮特殊样式 */
/* .admin-btn-disabled {
  position: relative;
  opacity: 0.7;
  background-color: rgba(222, 222, 222, 0.3);
  padding: 4px 10px;
  border-radius: 4px;
  border-left: 3px solid #ff6b00;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
} */

:deep(.el-tag--success) {
  background-color: rgba(255, 107, 0, 0.1);
  border-color: rgba(255, 107, 0, 0.2);
  color: #ff6b00;
}

:deep(.el-tag--info) {
  background-color: rgba(144, 147, 153, 0.1);
  border-color: rgba(144, 147, 153, 0.2);
  color: #909399;
}
</style> 